#include <stdio.h>
#include <string.h>

#define SIZE 81
#define LIMIT 20
#define swap(type, a, b)    do {type c=a; a=b; b=c;} while(0)

char * s_gets(char *, int);
void strsort(char **, int);

int main(void)
{
    int i;
    int ct;

    char input[LIMIT][SIZE];
    char * ptstr[LIMIT];

    printf("Input up to %d lines, and I will sort them.\n", LIMIT);
    printf("To stop, press the Enter key at a line's start.\n");

    ct = 0;
    while (ct < LIMIT && s_gets(input[ct], SIZE) != NULL && input[ct][0] != '\0')
    {
        ptstr[ct] = input[ct];
        ++ct;
    }

    strsort(ptstr, ct);

    printf("\nHere's the sorted list:\n");
    for (i=0; i<ct; ++i) {
        printf("%s\n", ptstr[i]);
    }

    return 0;
}

void strsort(char ** strs, int n)
{
    int i, j;
    /* Bubble Sort
    for (i=0; i<n-1; ++i){
        for (j=0; j<n-1-i; ++j) {
            if (strcmp(strs[j], strs[j+1]) > 0)
               swap(char *, strs[j], strs[j+1]);
        }
    }*/

    //Select Sort
    for (i=0; i<n-1; ++i)
        for (j=i+1; j<n; ++j)
            if (strcmp(strs[i], strs[j]) > 0)
                swap(char *, strs[i], strs[j]);
}

char * s_gets(char * str, int n)
{
    char * ret_val;
    int ct;

    ret_val = fgets(str, n, stdin);
    if (ret_val) {

        ct = 0;
        while (str[ct] != '\n' && str[ct] != '\0')
            ++ct;

        if (str[ct] == '\n')
            str[ct] = '\0';
        else
            while (getchar() != '\n')
                ;
    }

    return ret_val;
}
